package com.gongpingjialoan.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.gongpingjialoan.bean.AuthBen;
import com.gongpingjialoan.global.GPJApplication;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

import org.greenrobot.eventbus.EventBus;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GPJApplication.getInstance().wxApi.handleIntent(getIntent(), this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        GPJApplication.getInstance().wxApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        String typeStr = "";
        if (baseResp instanceof SendAuth.Resp) {
            typeStr = "授权";
        } else {
            typeStr = "分享";
        }
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if ("授权".equals(typeStr)) {
                    AuthBen  authBen = new AuthBen(((SendAuth.Resp) baseResp).code);
                    EventBus.getDefault().post(authBen);
                }else{
                    Toast.makeText(this, typeStr + "成功!", Toast.LENGTH_SHORT).show();
                }

                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if ("授权".equals(typeStr)) {
                    AuthBen  authBen = new AuthBen("");
                    EventBus.getDefault().post(authBen);
                }
                Toast.makeText(this, typeStr + "取消!", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                if ("授权".equals(typeStr)) {
                    AuthBen  authBen = new AuthBen("");
                    EventBus.getDefault().post(authBen);
                }
                Toast.makeText(this, typeStr + "被拒绝!", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                if ("授权".equals(typeStr)) {
                    AuthBen  authBen = new AuthBen("");
                    EventBus.getDefault().post(authBen);
                }
                Toast.makeText(this, typeStr + "错误!", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
